package com.dxt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dxt.common.AppConstant;
import com.dxt.common.LogHelper;
import com.dxt.common.MyBusiException;
import com.dxt.message.AppRequestMessage;
import com.dxt.message.ReponseMessage;
import com.dxt.netty.RuiKongYdServerHandler;
import com.dxt.netty.utils.CRC16Util;
import com.dxt.netty.utils.ReqStrUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service("ModifyThresholdValueService")
public class RKModifyThresholdValueService extends IBaseBusiService {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());

    @Autowired
    CacheManager cacheManager;

    @Override
    public ReponseMessage execute(String reqInfo, String url) {
        logger.debug(LogHelper._FUNC_START_());
        ReponseMessage message = new ReponseMessage();
        AppRequestMessage msg = getMsg();
        JSONObject jsonObject = JSON.parseObject(reqInfo);
        try {
            /**
             * 校验必填业务参数是否为空
             */
            checkParams(jsonObject, AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_DEVICEID);
        } catch (MyBusiException e) {
            message.setMsg(AppConstant.REPONSE_CODE.BUSI_ERROR, AppConstant.REPONSE_MSG.SYS_PARAM_ERROR_MSG,
                    e.getMessage());
            return message;
        }
        Channel channel = RuiKongYdServerHandler.getChannel(jsonObject.getLong(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_DEVICEID));
        if(channel == null){
            message = message.getReponseMessage(jsonObject,message);
            return message;
        }
        //将其转换为十六进制并输出
        String strHex = Long.toHexString(jsonObject.getLong(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_DEVICEID)).toUpperCase();
        logger.debug("strHex = " + ReqStrUtils.transHex(strHex));
        int random = new Random().nextInt(10);
        String signStr = "="+random+"Y4"+ReqStrUtils.transHex(strHex)+",";
        String U1 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U1);
        String U2 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U2);
        String U3 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U3);

        String U4 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U4);
        String U5 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U5);
        String U6 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U6);

        String I1 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_I1);
        String I2 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_I2);
        String I3 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_I3);

        String T1 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T1);
        String T2 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T2);
        String T3 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T3);
        String T4 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T4);

        String L1 = jsonObject.getString(AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_L1);
        if(!"".equals(U1) && U1 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U1 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(U1)))+"0,";
        }
        if(!"".equals(U2) && U2 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U2 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(U2)))+"0,";
        }
        if(!"".equals(U3) && U3 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U3 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(U3)))+"0,";
        }
        if(!"".equals(U4) && U4 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U4 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(U4)))+"0,";
        }
        if(!"".equals(U5) && U5 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U5 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(U5)))+"0,";
        }
        if(!"".equals(U6) && U6 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_U6 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(U6)))+"0,";
        }

        if(!"".equals(I1) && I1 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_I1 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(I1)))+"0,";
        }
        if(!"".equals(I2) && I2 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_I2 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(I2)))+"0,";
        }
        if(!"".equals(I3) && I3 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_I3 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(I3)))+"0,";
        }

        if(!"".equals(T1) && T1 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T1 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(T1)))+"0,";
        }
        if(!"".equals(T2) && T2 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T2 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(T2)))+"0,";
        }
        if(!"".equals(T3) && T3 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T3 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(T3)))+"0,";
        }
        if(!"".equals(T4) && T4 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_T4 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(T4)))+"0,";
        }

        if(!"".equals(L1) && L1 != null){
            signStr = signStr + AppConstant.REQUEST_REPONSE_PARAM.PARAM_IN_L1 + ReqStrUtils.transHex(Long.toHexString(Long.parseLong(L1)))+"0,";
        }
        message = message.getReponseMessage(signStr,channel,jsonObject,message);
        return message;
    }

}
